package com.palmdoit.common;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.ray.common.config.DBconfig;
import com.ray.common.config.MainConfig;

public class MyConfig extends MainConfig{
    
    private static Prop p;
    private WallFilter wallFilter;
    
    static void loadConfig() {
        if (p == null) {
            p = PropKit.use("config-dev.properties").appendIfExists("config-pro.properties");
        }
    }
    
    @Override
    public void configConstant(Constants me) {
    	DBconfig dbc = new DBconfig();//数据处理模式
    	dbc.setDelete(false);//数据是否真实删除
    	setDbconfig(dbc);
    	me.setResolveJsonRequest(true);
    	super.configConstant(me);
    }
    
    @Override
    public void configPlugin(Plugins me) {
        loadConfig();
        DruidPlugin dbPlugin = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
        wallFilter = new WallFilter();          // 加强数据库安全
        wallFilter.setDbType("mysql");
        dbPlugin.addFilter(wallFilter);
        dbPlugin.addFilter(new StatFilter());   // 添加 StatFilter 才会有统计数据
        //数据映射 配置ActiveRecord插件
        ActiveRecordPlugin arp=new ActiveRecordPlugin("palmdoit_server",dbPlugin);
        arp.setShowSql(p.getBoolean("devMode"));
        arp.setDialect(new MysqlDialect());
        dbPlugin.setDriverClass("com.mysql.jdbc.Driver");
        me.add(dbPlugin);
        me.add(arp);
        /********在此添加数据库 表-Model 映射*********/
        //如果使用了JFinal Model 生成器 生成了BaseModel 把下面注释解开即可
        super.configPlugin(me);
    }
    
    @Override
    public void configRoute(Routes me) {
        // TODO Auto-generated method stub
    	me.scan("com.palmdoit.controller");
        super.configRoute(me);
    }
    
    public static void main(String[] args) {
        //if(License.verify()) {
            UndertowServer server = UndertowServer.create(MyConfig.class,"undertow.properties").configWeb( builder -> {
                // 配置 Filter
                builder.addWebSocketEndpoint("com.ray.common.websocket.SysmsgWebsocket");
                builder.addWebSocketEndpoint("com.ray.common.websocket.ChatWebsocket");
            }).addHotSwapClassPrefix("com.github.jieblog.")
              .addHotSwapClassPrefix("com.ray.common.");
            server.start();
        //}
    }
}
